package com.ptkc.ext;

import com.ptkc.core.canal.handler.EventTypeHandler;
import com.ptkc.core.esearch.model.Comment;
import com.ptkc.core.esearch.repos.OrderRepository;
import com.ptkc.utils.NumberUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author: Mr.Jiao
 * @date: 2019年01月08日
 */
@Slf4j
@Component
public class OrderCommentCanalHandler extends EventTypeHandler<Comment> {

    @Autowired
    private OrderRepository orderRepository;

    public OrderCommentCanalHandler() {
        super("runfast", "runfast_business_comment");
    }

    @Override
    public Class<? extends Comment> getEntryClass() {
        return Comment.class;
    }

    @Override
    public void onInsert(Comment rowData) {
        this.onUpdate(rowData);
    }

    @Override
    public void onUpdate(Comment rowData) {
        if(rowData != null && NumberUtils.isPositiveInteger(rowData.getGoodsSellRecordId()) && NumberUtils.isRealNumber(rowData.getScore())){
            try {
                orderRepository.updateScore(rowData.getGoodsSellRecordId(), (int)Double.parseDouble(rowData.getScore()));
            } catch (IOException e) {
                e.printStackTrace();
                log.error(e.getMessage());
            }
        }
    }
}
